package com.tangkun.environment.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.ImageAdapter;
import com.tangkun.environment.entity.xinwen.ZiXunDetailReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;
import com.tangkun.environment.view.SquareGridView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 咨询详情
 */
public class ZiXunDetailActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_zixun_detail_riqi_xunwen)
    TextView tvZixunDetailRiqiXunwen;
    @BindView(R.id.tv_zixun_detail_content_xunwen)
    TextView tvZixunDetailContentXunwen;
    @BindView(R.id.sgv_zixun_detail_xunwen)
    SquareGridView squareGridViewXunWen;
    @BindView(R.id.tv_zixun_detail_riqi_huifu)
    TextView tvZixunDetailRiqiHuifu;
    @BindView(R.id.tv_zixun_detail_content_huifu)
    TextView tvZixunDetailContentHuifu;
    private Context mContext;

    /**
     * ID
     */
    private String id = "";
    /**
     * 询问照片列表适配器
     */
    private ImageAdapter adapterXunWen;
    /**
     * 询问图片数据源
     */
    private List<String> urlList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zixun_detail);
        mContext = this;
        ButterKnife.bind(this);
        id = getIntent().getStringExtra("id");
        title.setText("详情");
        initData();
    }

    private void initData() {
        adapterXunWen = new ImageAdapter(ZiXunDetailActivity.this, urlList);
        squareGridViewXunWen.setAdapter(adapterXunWen);
        questionDetail();
    }

    /**
     * 咨询详情
     */
    private void questionDetail() {
        fromNetwork.questionDetail(id).compose(new RxActivityHelper<ZiXunDetailReturn>().ioMain(ZiXunDetailActivity.this, true))
                .subscribe(new MySubscribe<ZiXunDetailReturn>(mContext, true) {
                    @Override
                    protected void success(ZiXunDetailReturn resultBean) {
                        if (resultBean.data != null) {
                            /************************************************************询问************************************************************/
                            //日期
                            if (!TextUtils.isEmpty(resultBean.data.createtime)) {
                                tvZixunDetailRiqiXunwen.setText(Utils.getDateYearMonthDayHourMinute(resultBean.data.createtime));
                            } else {
                                tvZixunDetailRiqiXunwen.setText("");
                            }
                            //内容
                            if (!TextUtils.isEmpty(resultBean.data.content)) {
                                tvZixunDetailContentXunwen.setText(resultBean.data.content);
                            } else {
                                tvZixunDetailContentXunwen.setText("");
                            }
                            // 图片
                            urlList.clear();
                            if (!TextUtils.isEmpty(resultBean.data.img)) {
                                String[] imgArray = resultBean.data.img.split(",");
                                urlList = Arrays.asList(imgArray);//数组转集合
                            }
                            if (urlList.size() > 0) {
                                squareGridViewXunWen.setVisibility(View.VISIBLE);
                                adapterXunWen = new ImageAdapter(ZiXunDetailActivity.this, urlList);
                                if (urlList.size() == 1) {
                                    squareGridViewXunWen.setNumColumns(1);
                                    Utils.setGridViewHeightBasedOnChildren(squareGridViewXunWen, 1);
                                } else if (urlList.size() == 2) {
                                    squareGridViewXunWen.setNumColumns(2);
                                    Utils.setGridViewHeightBasedOnChildren(squareGridViewXunWen, 2);
                                } else if (urlList.size() == 3) {
                                    squareGridViewXunWen.setNumColumns(3);
                                    Utils.setGridViewHeightBasedOnChildren(squareGridViewXunWen, 3);
                                } else if (urlList.size() == 4) {
                                    squareGridViewXunWen.setNumColumns(2);
                                    Utils.setGridViewHeightBasedOnChildren(squareGridViewXunWen, 2);
                                } else {
                                    squareGridViewXunWen.setNumColumns(3);
                                    Utils.setGridViewHeightBasedOnChildren(squareGridViewXunWen, 3);
                                }
                                squareGridViewXunWen.setAdapter(adapterXunWen);
                            } else {
                                squareGridViewXunWen.setVisibility(View.GONE);
                            }

                            /************************************************************回复************************************************************/
                            //日期
                            if (!TextUtils.isEmpty(resultBean.data.answertime)) {
                                tvZixunDetailRiqiHuifu.setText(Utils.getDateYearMonthDayHourMinute(resultBean.data.answertime));
                            } else {
                                tvZixunDetailRiqiHuifu.setText("");
                            }
                            //内容
                            if (!TextUtils.isEmpty(resultBean.data.answer)) {
                                tvZixunDetailContentHuifu.setText(resultBean.data.answer);
                            } else {
                                tvZixunDetailContentHuifu.setText("");
                            }
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }


    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                JumpHelper.finish(mContext);
                break;
        }
    }
}
